//
//  GSDKComplatform+IAP.m
//  GameSDK
//
//  Created by Johnny on 2017/7/28.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKComplatform+IAP.h"
#import "GSDKServicesManager.h"
#import "GSDKCommonMethod.h"
#import "YYKit.h"
#import "GSDKComplatformBase.h"
#import "GSDKActivityIndicatorView.h"
#import "GSDKComplatformBase.h"
#import "RMStore.h"
#import "GSDKIAPController.h"
#import "GSDKWebViewController.h"
#import "UIViewController+Extension.h"
#import "GSDKRequestModel.h"
@implementation GSDKComplatform (IAP)
- (void)getGoods {
    [[GSDKServicesManager new] request:[[GSDKGoodsConfigtModel alloc] initWithToken:GSDKCOMMONMETHOD.GSDKToken] andCompletionBlock:^(id responseObject) {
        DEBUGMSG(@"responseObject = %@", responseObject);
        GSDKResGoodsConfigModel * model = [GSDKResGoodsConfigModel modelWithJSON:responseObject];
        if ([model.code isEqualToString:@"0"]) {
            if (model.goods_list.count > 0) {
                GSDKIAPStatus * user = [[GSDKIAPStatus alloc] init];
                user.gSDKIAPStatus = GSDK_IAP_STATUS_PRODUCTLIST;
                GSDKMessageStatus * message = [[GSDKMessageStatus alloc] init];
                message.code = @"0";
                message.msg = [NSString localizedStringForKey:@"GSDK_getgoodssuccess_text" value:@"获取商品列表成功"];
                [[NSNotificationCenter defaultCenter] postNotificationName:GSDKIAPDidFinishNotification object:nil userInfo:@{@"GSDKIAPStatus": user, @"GSDKMessageStatus": message, @"goods": model.goods_list}];
            } else {
                GSDKIAPStatus * user = [[GSDKIAPStatus alloc] init];
                user.gSDKIAPStatus = GSDK_IAP_STATUS_PRODUCTLIST;
                GSDKErrorStatus * error = [[GSDKErrorStatus alloc] init];
                error.gSDKErrorStatus = GSDK_ERROR_STATUS_PRODUCTLIST_EMPTY;
                GSDKMessageStatus * message = [[GSDKMessageStatus alloc] init];
                message.code = @"-1";
                message.msg = [NSString localizedStringForKey:@"GSDK_goodsempty_error" value:@"商品列表为空"];
                [[NSNotificationCenter defaultCenter] postNotificationName: GSDKIAPDidFinishNotification object:nil userInfo:@{@"GSDKIAPStatus": user, @"GSDKErrorStatus": error, @"GSDKMessageStatus": message}];
            }
        } else {
            GSDKIAPStatus * user = [[GSDKIAPStatus alloc] init];
            user.gSDKIAPStatus = GSDK_IAP_STATUS_PRODUCTLIST;
            GSDKErrorStatus * error = [[GSDKErrorStatus alloc] init];
            error.gSDKErrorStatus = GSDK_ERROR_STATUS_SERVICE;
            GSDKMessageStatus * message = [[GSDKMessageStatus alloc] init];
            message.code = @"-1";
            message.msg = model.message;
            [[NSNotificationCenter defaultCenter] postNotificationName: GSDKIAPDidFinishNotification object:nil userInfo:@{@"GSDKIAPStatus": user, @"GSDKErrorStatus": error, @"GSDKMessageStatus": message}];
        }
    }];
}

- (void)buyWithGoods:(nonnull GSDKGoods*)goods
          andOrderId:(nonnull NSString *)orderId
        andGameOther:(nonnull NSString *)gameOther
          andSandbox:(BOOL)isSandbox; {
    [[GSDKActivityIndicatorView shared] show];
    
    if (![GSDKCOMPLATFORM isLogin]) {
        [[GSDKActivityIndicatorView shared] hide];
        
        GSDKIAPStatus * user = [[GSDKIAPStatus alloc] init];
        user.gSDKIAPStatus = GSDK_IAP_STATUS_PRODUCTBUY;
        GSDKErrorStatus * error = [[GSDKErrorStatus alloc] init];
        error.gSDKErrorStatus = GSDK_ERROR_STATUS_NOLOGIN;
        GSDKMessageStatus * message = [[GSDKMessageStatus alloc] init];
        message.code = @"-1";
        message.msg = [NSString localizedStringForKey:@"GSDK_nologin_error" value:@"没有登录"];
        [[NSNotificationCenter defaultCenter] postNotificationName: GSDKIAPDidFinishNotification object:nil userInfo:@{@"GSDKIAPStatus": user, @"GSDKErrorStatus": error, @"GSDKMessageStatus": message}];
        return;
    }
    if (GSDKCOMMONMETHOD.versionModel.is_purl && GSDKCOMMONMETHOD.versionModel.is_purl.length != 0) {
        //走第三方支付
        GSDKWebViewController * vc = [GSDKWebViewController new];
        NSString * urlString = GSDKCOMMONMETHOD.versionModel.is_purl;
        if (urlString == nil) { urlString = @"http://api.zy1game.com/api/ocean_payment"; }
        if (![urlString hasSuffix:@"?"]) { urlString = [NSString stringWithFormat:@"%@?", urlString]; }
        
        GSDKWebPayModel * model = [[GSDKWebPayModel alloc] initIAPCallBackWithSid:GSDKCOMMONMETHOD.GSDKSid andToken:GSDKCOMMONMETHOD.GSDKToken andGameOrderId:orderId andGameOther:gameOther andSandbox:isSandbox ? @"1" : @"0" andGoodsId:goods.goods_id andGoodsName:goods.goods_name andGoodsPrice:goods.goods_price andPayMoney:goods.goods_price andLoginToken: GSDKCOMMONMETHOD.accountModel.token];
        [vc reloadAtUrl:[NSString stringWithFormat:@"%@%@", urlString, [model getUrlParameter]]];
        UIViewController * viewController = GSDKCOMMONMETHOD.rootViewController;
        if (viewController == nil) {
            viewController = [UIViewController getCurrentRootViewController];
        }
        [viewController presentViewController:vc animated:true completion:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:GSDKToolBarDidFinishNotification object:@YES userInfo:nil];
    } else {
        //走苹果内购
        if (![SKPaymentQueue canMakePayments]) {
            [[GSDKActivityIndicatorView shared] hide];
            
            GSDKIAPStatus * user = [[GSDKIAPStatus alloc] init];
            user.gSDKIAPStatus = GSDK_IAP_STATUS_PRODUCTBUY;
            GSDKErrorStatus * error = [[GSDKErrorStatus alloc] init];
            error.gSDKErrorStatus = GSDK_ERROR_STATUS_BUY_ERROR;
            GSDKMessageStatus * message = [[GSDKMessageStatus alloc] init];
            message.code = @"-1";
            message.msg = [NSString localizedStringForKey:@"GSDK_noiap_error" value:@"不允许程序内付费"];
            [[NSNotificationCenter defaultCenter] postNotificationName: GSDKIAPDidFinishNotification object:nil userInfo:@{@"GSDKIAPStatus": user, @"GSDKErrorStatus": error, @"GSDKMessageStatus": message}];
            return;
        }
        
        [GSDKIAP buyWithGoods:goods andOrderId:orderId andGameOther:gameOther andSandbox:isSandbox];
    }
}
@end


@implementation GSDKIAPStatus
@synthesize gSDKIAPStatus;
@end
